﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntitiesLayer;
using System.Transactions;
using System.Data.SqlClient;
using System.Data;
namespace DAL.Repositories
{
    class EstrellaRepository: IRepository<Estrella>
    {
        private IList<IEntity> insertedList;
        private IList<IEntity> updatedList;
        private IList<IEntity> deletedList;

        internal EstrellaRepository()
        {

            insertedList = new List<IEntity>();
            updatedList = new List<IEntity>();
            deletedList = new List<IEntity>();
        }

        public void Insert(Estrella entity)
        {
            insertedList.Add(entity);
        }

        public void Delete(Estrella entity)
        {
            deletedList.Add(entity);
        }

        public void Update(Estrella entity)
        {
            updatedList.Add(entity);
        }

        public IEnumerable<Estrella> GetAll()
        {
            SqlCommand cmd = new SqlCommand();
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "pa_listar_estrellas");

            foreach (DataRow dr in ds.Tables[0].Rows)               
                yield return crearEstrella(dr, Convert.ToInt32(dr["IDESTRELLA"])); 



        }

        public Estrella GetById(int id)
        {
            Estrella objEstrella = null;
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.Add(new SqlParameter("@IdEstrella", id));
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "pa_consultar_estrella_porID");

            foreach (DataRow dr in ds.Tables[0].Rows)
                objEstrella = crearEstrella(dr, id);


            return objEstrella;

        }

        public void Save()
        {
            using (TransactionScope scope = new TransactionScope())
            {

                try
                {

                    foreach (Estrella p in insertedList)
                        InsertSatelite(p);

                    foreach (Estrella p in updatedList)
                        UpdateSatelite(p);

                    foreach (Estrella p in deletedList)
                        DeleteSatelite(p);


                    scope.Complete();
                }
                catch (TransactionAbortedException ex)
                {
                    Console.WriteLine(ex.Message);
                    throw ex;
                }
                catch (ApplicationException ex)
                {
                    Console.WriteLine(ex.Message);
                    throw ex;
                }
                finally
                {
                    Clear();
                }

            }
        }

        public void Clear()
        {
            insertedList.Clear();
            updatedList.Clear();
            deletedList.Clear();
        }
        #region Utilities

        internal static Estrella crearEstrella(DataRow dr, int id)
        {
            // puede que el timespan no tenga un constructor para los ticks

            Estrella objEstrella = new Estrella(

                       id: id,
                      nombre: dr["NOMBRE"].ToString(),
                      masa: Convert.ToDecimal(dr["MASA"]),
                      temperaturaMedia: Convert.ToDecimal(dr["TEMPMEDIA"]),
                      duracion: new TimeSpan(Convert.ToInt64(dr["DURACIONDIA"])),
                      edad: Convert.ToInt32(dr["EDAD"]),
                      composicion: dr["COMPOSICION"].ToString(),
                      intensidadLuminica: Convert.ToDecimal(dr["INTENSIDADLUMINICA"]),
                      tamano: Convert.ToDecimal(dr["TAMANO"]),
                      missionDelegate: new Lazy<IList<Mission>>(() => { 
                          return MissionRepository.consultarMissionesCuerpoCeleste(id); 
                      })
                      


                  );

            return objEstrella;
        }


        private void InsertSatelite(Estrella objEstrella)
        {
            try
            {
                //falta la duracionano 
                SqlCommand cmd = new SqlCommand();
                cmd.Parameters.Add(new SqlParameter("@nombre", objEstrella.Nombre));
                cmd.Parameters.Add(new SqlParameter("@masa", objEstrella.Masa));
                cmd.Parameters.Add(new SqlParameter("@temp", objEstrella.TemperaturaMedia));
                cmd.Parameters.Add(new SqlParameter("@edad", objEstrella.Edad));
                cmd.Parameters.Add(new SqlParameter("@duracion", objEstrella.Duracion.Ticks));
                cmd.Parameters.Add(new SqlParameter("@composicion", objEstrella.Composicion ));
                cmd.Parameters.Add(new SqlParameter("@intensidad", objEstrella.IntensidadLuminica));
                cmd.Parameters.Add(new SqlParameter("@tamano", objEstrella.Tamano));
                DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "pa_RegistrarEstrella");
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }

        }

        private void UpdateSatelite(Estrella objSatelite)
        {
            throw new NotImplementedException();

        }

        private void DeleteSatelite(Estrella objSatelite)
        {
            throw new NotImplementedException();

        }
    }
}
        #endregion Utilities